package com.kbook.action.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.kbook.bean.UserBean;
import com.kbook.dao.CategoryDao;
import com.kbook.dao.ChapterDao;
import com.kbook.dao.ChartsDao;
import com.kbook.dao.NovelDao;
import com.kbook.dao.impl.CategoryDaoImpl;
import com.kbook.dao.impl.ChapterDaoImpl;
import com.kbook.dao.impl.ChartsDaoImpl;
import com.kbook.dao.impl.NovelDaoImpl;
import com.kbook.log.LogWriter;
import com.kbook.tool.Constants;
import com.kbook.tool.Tools;
import com.kbook.vo.Category;
import com.kbook.vo.Chapter;
import com.kbook.vo.Charts;
import com.kbook.vo.Novel;

public class IntroductionAction extends AbstractAction {
	
	private static final Logger logger = Logger.getLogger("IntroductionAction");

	public Object doAction(UserBean user, Map params) throws Exception {
		// TODO Auto-generated method stub
		Map introductionMap = new HashMap();
		
		int novelId =Tools.parseInt(params.get("novelId")+"");
		
		NovelDao novelDao = new NovelDaoImpl();
		Novel novel = novelDao.query(novelId);
		
		ChapterDao chapterDao = new ChapterDaoImpl();
		List<Chapter> chapterList = chapterDao.queryAll(novelId);
		
		CategoryDao categoryDao = new CategoryDaoImpl();
		Category category = categoryDao.query(novel.getCategoryId());
		
		List<Category> categoryList = categoryDao.queryAll();
		
		ChartsDao chartDao = new ChartsDaoImpl();
		List<Charts> hotestChartList = chartDao.queryById(Constants.CHART_HOTEST);
		
		introductionMap.put("firstChapter", chapterList!=null&&chapterList.size()>0?chapterList.get(0):null);
		introductionMap.put("categoryList", categoryList);
		introductionMap.put("category", category);
		introductionMap.put("novel", novel);
		introductionMap.put("chapterList", chapterList);
		introductionMap.put("hotestChartList", hotestChartList);
		
		return introductionMap;
	}

	@Override
	protected void afterAction(Object obj, UserBean user, Map params) {
		// TODO Auto-generated method stub
		Map resultMap = (Map)obj;
		
		List<String> logParams = new ArrayList<String>();
		logParams.add((String)params.get("ip"));
		logParams.add((String)params.get("ua"));
		logParams.add((String)params.get("cid"));
		Category category = (Category)resultMap.get("category");
		logParams.add(category.getId()+"");
		logParams.add((String)params.get("novelId"));
		Novel novel = (Novel)resultMap.get("novel");
		logParams.add(novel.getId()+"");
		logParams.add(novel.getStatus()+"");

		LogWriter.getInstance().createLog(logParams, logger);
	}

	
	

}
